﻿using MediatR;
using SqlSugar;
using WarehouseManagement.Application.Command.WarehouseCommand;
using WarehouseManagement.Domain.DTO.Warehouse;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.WarehouseCommandHandler
{
    /// <summary>
    /// 显示仓库信息
    /// </summary>
    public class WarehouseSelectCommandHandler : IRequestHandler<WarehouseSelectCommand, Warehousepage>
    {
        private readonly IWarehouseRepository _warehouseRep;

        public WarehouseSelectCommandHandler(IWarehouseRepository warehouseRep)
        {
            this._warehouseRep = warehouseRep;
        }

        public async Task<Warehousepage> Handle(WarehouseSelectCommand request, CancellationToken cancellationToken)
        {
            Warehousepage page = new Warehousepage();
            var list=(await _warehouseRep.GetAll()).Where(t=>t.Office_Id==request.Office_Id&&t.IsDel==false);
            if (!string.IsNullOrEmpty(request.WNumber))
            {
                list = list.Where(t => t.WNumber.Contains(request.WNumber));
            }
            if (!string.IsNullOrEmpty(request.WName))
            {
                list = list.Where(t => t.WName.Contains(request.WName));
            }
            if (request.WType > 0)
            {
                list = list.Where(t => t.WType == request.WType);
            }
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(page.listcount * 1.0 / request.PageSize);
            page.list = list.OrderByDescending(t => t.WarehouseId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }

        
    }
}
